var request = require('request');
var qs = require('qs');

exports.status = function(req,res){//主页状态查询
    if(req.session.token) {
        req.session.destroy();//用户返回登陆界面时销毁session
    }
    var url = 'http://39.106.9.239/ATM/tokens/checkStatus';
    var formData = {
        "deviceNo" : "100"
    }
    request({
        url: url,
        method: "post",
        headers: {
            "content-type" : "application/x-www-form-urlencoded"
        },
        body: qs.stringify(formData)
    },function(_err,_res,_resBody) {
        console.log(_resBody);
        var body = JSON.parse(_resBody);
        if(!_err && _res.statusCode == 200) {
            switch(body.code) {
                case 0:
                res.sendFile(__dirname + "/src/templates/index.html" );
                break;
                case 711:
                res.sendFile(__dirname + "/src/templates/NeedFix.html")
                break;
            }
        }
    })
}

exports.login = function(req,res) { //登录
    var url = 'http://39.106.9.239/ATM/tokens/login';
    var formData = {
        "cardNo" : req.body.cardNo,
        "password" : req.body.password
    }
  //账号验证。。。
    request({
        url: url,
        method: "post",
        headers: {
            "content-type" : "application/x-www-form-urlencoded"
        },
        body: qs.stringify(formData)
    },function(_err,_res,_resBody){
        var body = JSON.parse(_resBody); 
        if(!_err && _res.statusCode == 200) {
            switch(body.code) {
                case 0:
                req.session.cardNo = body.data.cardNo;//可能bug
                req.session.token = body.data.token;
                res.send(_resBody);
                break;
                case 114: //冻结
                case 123://密码错误
                case 113://账号不存在
                res.send(_resBody);
                break;
            }
        }
    })
}

exports.loading = function(req,res) {  //loadin页面
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/loading.html")
    }else {
        res.redirect("./");
    }
    
}
exports.business = function(req,res) { //业务页面
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/business.html")
    }else {
        res.redirect("./");
    }
}

exports.logout = function(req,res) { //登出
    if(req.session.token) {
        var url = 'http://39.106.9.239/ATM/tokens/logout';
    request({
        url: url,
        method: "post",
        headers: {
            "authorization" : req.session.token,
            "deviceNo" : 100
        }
    },function(_err,_res,_resBody){
        if(!_err && _res.statusCode == 200) {
            req.session.destroy();
            res.redirect("/");
        }else {
            console.log(_err);
        }
    });
    }else {
        res.redirect("./");
    }
}

exports.withdraw = function(req,res) { //取款
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/Withdraw.html")
    }else {
        res.redirect("./");
    }
}
exports.withdraw_2 = function(req,res) {//取款数据
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/withdraw";
        var data = {
            "amount" : req.body.money
        }
        request({
            url: url,
            method: "post",
            headers: {
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100
            },
            body: qs.stringify(data)
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
                req.session.adviceId = JSON.parse(_resBody).data.adviceId;
                res.send(_resBody);
            }else {
                console.log(_err);
            }
        });
    }else {
        res.redirect("./");
    }
}
exports.successfulWithdraw = function(req,res) {//取款成功
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/successfulWithdraw.html")
    }else {
        res.redirect("./");
    }
}

exports.frozen = function(req,res) { //冻结
    res.sendFile(__dirname + "/src/templates/frozen.html");

}

exports.balance = function(req,res) {  //查额
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/balance.html")
    }else {
        res.redirect("./");
    }
}
exports.balance_2 = function(req,res) { //初始化查额页数据
    var url = "http://39.106.9.239/ATM/business/query/accountBalance";
    if(req.session.token) {
        request({
            url: url,
            method : "get",
            headers: {
                "authorization" : req.session.token,
                "deviceNo" : 100
            }
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
               res.send(_resBody);
            }else {
                console.log(_err);
            }
        });
    }else {
        res.redirect("./");
    }
}

exports.account = function(req,res) {//转账
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/account.html")
    }else {
        res.redirect("./");
    }
}
exports.account_2 = function(req,res) { //转账账户
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/transfer/checkToAccount";
        var data ={
            "tocardNo" : req.body.tocardNo
        }
        request({
            url: url,
            method: "post",
            headers: { 
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100,
            },
            body: qs.stringify(data)
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
                if(JSON.parse(_resBody).code == 0) {
                    req.session.username = JSON.parse(_resBody).data.username;
                    req.session.tocardNo = JSON.parse(_resBody).data.tocardNo;
                }
                console.log(JSON.parse(_resBody).code);
                res.send(_resBody);
             }else {
                 console.log(_err);
             }
        });
    }else {
        res.redirect("./");
    }
}
exports.transfer = function(req,res) {//转账金额
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/transfer.html")
    }else {
        res.redirect("./");
    }
}
exports.money = function(req,res) {//保存转账金额
    if(req.session.token) {
        req.session.amount = req.body.amount;
        res.end();
    }else {
        res.redirect("./");
    }
}
exports.confirm = function(req,res) {//转账信息确认
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/confirm.html")
    }else {
        res.redirect("./");
    }
}
exports.confirm2 = function(req,res) {//转账信息确认初始化
    if(req.session.token) {
        var data = {
            "tocardNo" : req.session.tocardNo,
            "username" : req.session.username,
            "amount" : req.session.amount,
            "fee" : 0
        }
        res.send(JSON.stringify(data));
    }else {
        res.redirect("./");
    }
}
exports.password = function(req,res) {//转账密码
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/password.html");
    }else {
        res.redirect("./");
    }
}
exports.password_2 = function(req,res) {//转账密码ajax
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/transfer/checkpwd";
        var data ={
            "tocardNo" : req.session.tocardNo,
            "amount" : req.session.amount,
            "password" : req.body.password
        }
        request({
            url: url,
            method: "post",
            headers: { 
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100,
            },
            body: qs.stringify(data)
        },function(_err,_res,_resBody) {
            var responseBody = JSON.parse(_resBody);
            if(!_err && _res.statusCode == 200) {
                if(responseBody.data.adviceId) {  //待修改
                    req.session.adviceId = JSON.parse(_resBody).data.adviceId;//凭条id
                }
                res.send(_resBody);
             }else {
                 console.log(_err);
             }
        });
    }else {
        res.redirect("./");
    }
}

exports.successfultransfer = function(req,res) {//转账成功页面
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/successfultransfer.html");
    }else {
        res.redirect("./");
    }
}
exports.successfultransfer_2 = function(req,res) {//转账成功页面
    if(req.session.token) {
        var data = {
            "amount" : req.session.amount,
            "fee" : 0
        }
        res.send(JSON.stringify(data));
    }else {
        res.redirect("./");
    }
}

exports.detailed = function(req,res) {//明细查询
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/detailed.html");
    }else {
        res.redirect("./");
    }
}
exports.detailed_2 = function(req,res) { //明细数据
    var url = "http://39.106.9.239/ATM/business/query/statement"
    if(req.session.token) {
        request({
            url: url,
            method : "get",
            headers: {
                "authorization" : req.session.token,
                "deviceNo" : 100
            }
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
               res.send(_resBody);
            }else {
                console.log(_err);
            }
        });
    }else {
        res.redirect("./");
    }
}


exports.deposit = function(req,res) {//存款
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/deposit.html");
    }else {
        res.redirect("./");
    }
}
exports.deposit_2 = function(req,res) {//存款数据
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/deposit";
        var formData = {
            "amount" : req.body.Money,
        }
        request({
            url: url,
            method: "post",
            headers: {
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100
            },
            body: qs.stringify(formData)
        },function(_err,_res,_resBody){
            if(!_err && _res.statusCode == 200) {
                if(JSON.parse(_resBody).code == 0) {
                    req.session.count = JSON.parse(_resBody).data.count;
                    req.session.amount = JSON.parse(_resBody).data.amount;
                    req.session.adviceId = JSON.parse(_resBody).data.adviceId;
                }
                res.send(_resBody);
             }else {
                 console.log(_err);
             }
         });
    }else {
        res.redirect("./");
    }
}
exports.depositSuccess = function(req,res){//存款成功
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/depositSuccess.html");
    }else {
        res.redirect("./");
    }
}
exports.depositSuccess_2 = function(req,res) {//存款成功数据
    if(req.session.token) {
       var data = {
           "data" : {
               "count" : req.session.count,
               "amount" : req.session.amount
           }
       }
       res.send(JSON.stringify(data));
    }else {
        res.redirect("./");
    }
}

exports.changePassword = function(req,res) {//改密
    if(req.session.token) {
        res.sendFile(__dirname + "/src/templates/changePassword.html");
    }else {
        res.redirect("./");
    }
}
exports.changepwd = function(req,res) {//改密
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/user/changepwd";
        var data = {
            "oldpwd" : req.body.oldpass,
            "newpwd1" : req.body.newpass,
            "newpwd2" : req.body.renew
        }
       request({
           url : url,
           method : "post",
           headers : {
            "content-type" : "application/x-www-form-urlencoded",
            "authorization" : req.session.token,
            "deviceNo" : 100, 
           },
           body: qs.stringify(data)
       },function(_err,_res,_resBody) {
        if(!_err && _res.statusCode == 200) {
            res.send(_resBody);
         }else {
             console.log(_err);
         }
       });
    }else {
        res.redirect("./");
    }
}
exports.successpass = function(req,res) {//改密成功
    if(req.session.token) {
        req.session.destroy();
        res.sendFile(__dirname + "/src/templates/successpass.html");
    }else {
        res.redirect("./");
    }
}

exports.advice = function(req,res) {//打印凭条
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/query/advice/"+req.session.adviceId;
        
        console.log(url);
        request({
            url : url,
            method: "get",
            headers: {
                "authorization" : req.session.token,
                "deviceNo" : 100, 
            }       
        },function(_err,_res,_resBody) {
            res.send(_resBody);
        });
    }else {
        res.redirect("./");
    }
}

